package com.em.sugarfight.entity;

import lombok.Data;

/**
 * 危险地图 - 评估每个位置的威胁级别
 */
@Data
public class DangerMap {
    private double[][] dangerLevel;
    private int width, height;

    public DangerMap(int width, int height) {
        this.width = width;
        this.height = height;
        this.dangerLevel = new double[width][height];
    }

    /**
     * 标记炸弹威胁区域
     */
    public void markBombDanger(Bomb bomb,int currentTick) {
        Position bombPos = bomb.getPosition();
        int range = bomb.getRange();
        int explodeAt = bomb.getExplodeAt();
        int bombTickSub = explodeAt - currentTick;

        // 炸弹中心点最危险
        setDangerLevel(bombPos.getX(), bombPos.getY(), calculateBombThreat(bombTickSub));

        // 标记爆炸范围内的四个方向
        markBombLine(bombPos.getX(), bombPos.getY(), range, 1, 0, bombTickSub);   // 向右
        markBombLine(bombPos.getX(), bombPos.getY(), range, -1, 0, bombTickSub);  // 向左
        markBombLine(bombPos.getX(), bombPos.getY(), range, 0, -1, bombTickSub);   // 向下
        markBombLine(bombPos.getX(), bombPos.getY(), range, 0, 1, bombTickSub);  // 向上
    }

    private void markBombLine(int startX, int startY, int range, int dx, int dy, int explodeAt) {
        for (int i = 1; i <= range; i++) {
            int x = startX + dx * i;
            int y = startY + dy * i;

            if (!isValidPosition(x, y)) break;

            double threat = calculateBombThreat(explodeAt);
            addDangerLevel(x, y, threat);
        }
    }

    /**
     * 根据爆炸时间计算威胁程度
     */
    private double calculateBombThreat(int explodeAt) {
        // 爆炸时间越近，威胁越大
        // 假设当前tick为0基准
        if (explodeAt <= 5) return 100.0; // 即将爆炸
        if (explodeAt <= 10) return 50.0;  // 很危险
        if (explodeAt <= 20) return 20.0;  // 中等危险
        return 1.0; // 低危险
    }

    /**
     * 标记敌人可能放炸弹的区域
     */
    public void markPotentialBombZones(PlayerStateVo enemy) {
        PixelPosition enemyPos = enemy.getPosition();

        // 在敌人周围标记潜在威胁
        for (int dx = -2; dx <= 2; dx++) {
            for (int dy = -2; dy <= 2; dy++) {
                int x = enemyPos.getY() + dx;
                int y = enemyPos.getX() + dy;

                if (isValidPosition(x, y)) {
                    // 根据距离计算潜在威胁
                    double distance = Math.abs(dx) + Math.abs(dy);
                    double threat = Math.max(0, 3.0 - distance);
                    addDangerLevel(x, y, threat);
                }
            }
        }
    }

    public boolean isInDanger(Position pos) {
        return getRiskLevel(pos) > 10.0; // 威胁阈值
    }

    public double getRiskLevel(Position pos) {
        if (!isValidPosition(pos.getX(), pos.getY())) return Double.MAX_VALUE;
        return dangerLevel[pos.getY()][pos.getX()];
    }

    private void setDangerLevel(int x, int y, double level) {
        if (isValidPosition(x, y)) {
            dangerLevel[y][x] = Math.max(dangerLevel[y][x], level);
        }
    }

    private void addDangerLevel(int x, int y, double level) {
        if (isValidPosition(x, y)) {
            dangerLevel[y][x] += level;
        }
    }

    private boolean isValidPosition(int x, int y) {
        return x >= 0 && y < width && y >= 0 && x < height;
    }
}
